-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the fix! LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this ! I put little more thought into this . All we need is to check whether func (try catch around func for dmlc::Error) and save the exception. check if exception_ptr is set in PushSync and if so rethrow exception inside PushSync. This should handle both 1 and 2 mentioned here: #14522
EDIT: Approach I mentioned will still only work for 1. We will hit situation 2 when there is no sync call in the forward or backward. I think custom op wasn't designed to handle this use case and it is tricky to support it now. I looked at all examples, tests and also customer code sockeye, and most of these codes have some sync call at the end like self.assign
assigning output ndarrays. We should make this limitation explicit in CustomOp documentation for now.
src/operator/custom/custom-inl.h
Outdated
fn(); | ||
try { | ||
task.fn(); | ||
} catch (dmlc::Error& e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about other types of exceptions? Could we add a comment in the code clarifying this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What other types? I think the only valid exception is dmlc::Error, other exception means wrong code.
@@ -5237,6 +5238,17 @@ def custom_add(): | |||
p.join(5) | |||
assert not p.is_alive(), "deadlock may exist in custom operator" | |||
|
|||
# test except handling | |||
# see https://github.com/apache/incubator-mxnet/pull/14575 | |||
def custom_add_exc(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a comment that an exception is expected due to shapes I assume?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll add it.
@mxnet-label-bot Add [Backend, Operator] |
@anirudh2290 I've simplified the exception catching according to your suggestion. Agreed that the situation 2 is really tricky, because for Now |
src/engine/threaded_engine.cc
Outdated
BulkFlush(); | ||
ThreadedVar* threaded_var = ThreadedVar::CastFromBase(var); | ||
if (threaded_var->ready_to_read()) { | ||
ThrowException(threaded_var); | ||
CustomOperator::Get()->ThrowException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets remove this. ThreadedEngine should not depend on custom operator code.
std::make_shared<std::exception_ptr>(std::current_exception()); | ||
ctx.async_on_complete(); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can solve both 1 and 2 this way: After func is called do wait_to_read on all elements in arrs. Then catch and save. Remove lines 104 and 105. In PushSync, check if exception is set and rethrow exception. Also catch it and call async_on_complete in pushsync. and return.
Something like the following:
Engine::Get()->PushSync(
[=](RunContext rctx) {
try {
if (exception_) {
std::rethrow_exception(exception_);
}
} catch(dmlc::Error& err) {
ctx.async_on_complete(&err);
return;
}
}
Thanks to this support added for horovod: #13932 we may be able to leverage this to call async_on_complete with the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding wait_to_read in custom op can solve 1&2, and it can be treated as normal op without using ExecType::kAsync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we probably still need PushSync for the Sparse ndarray updates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we still need ExecType::kAsync. Custom operator is still async and when push is called it just pushes it into its custom op worker queue for execution later. Async will ensure that the threaded_engine_pooled and threaded_engine_per_device treat it as a special case and execute immediately instead of pushing the work again to one of the engine worker thread queue. Pushing to engine worker thread queue is unnecessary for custom op.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After testing, ExecType::kAsync is really needed. Adding wait_to_read in engine worker thread will cause deadlock.
But PushSync can be removed and works well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we probably still need it for sparse. since for sparse we are updating chunk it is a write option. WaitToRead may not be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I also add WaitToWrite to make sure there's no left out exceptions.
overall looks good. can you run the example/reinforcement-learning/dqn which includes a custom op on CPU to check for performance and make sure there is no perf impact.
@anirudh2290 I'll test the example you've mentioned. Let me mention another problem here:
Run the code with:
Even if you have n threads, we can still deadlock the execution with n+1 custom op dependency chain: A->B->C... We should make sure there are always enough threads to use, maybe remove MXNET_CUSTOM_OP_NUM_THREADS and create new thread when in need? |
@arcadiaphy Custom Operator creates new thread when in need now, and MXNET_CUSTOM_OP_NUM_THREADS is the maximum number of threads. Removing this line https://github.com/apache/incubator-mxnet/blob/master/src/operator/custom/custom-inl.h#L185 to support unlimited number of threads. |
@arcadiaphy Yes good point, the code you linked will cause a deadlock. this is also true if custom op forward or backward has asnumpy() or wait_to_read at the end even before applying your change, when you change MXNET_CUSTOM_OP_NUM_THREADS=1. Even before the custom op multi threaded change this was the case. Also, I am concerned about the performance because of the blocking call during op execution. I think we have reached a point where there is no option but to change the approach and special case "CustomOperator" in ExecuteOprBlock by checking whether op is CustomOperator and if so, execute the operator instead of skipping (https://github.com/apache/incubator-mxnet/blob/master/src/engine/threaded_engine.h#L377) . I was trying to avoid this to keep the engine agnostic of the op its executing but this seems like the least impactful solution currently. |
@anirudh2290 If the engine code remains unchanged, I think enabling unlimited number of custom threads when needed is a quick fix. |
Also for unlimited threads, theoretically, there should be no performance impact. |
I think we should make the modification in engine. Keeping that env variable is fine as Users may want to limit the custom op threads, so that customers don't run into situation where there are too many custom ops in their model and lot of threads are spawned and thread spawning overhead being high. Also, performance wise adding the waittoread and waittowrite can impact performance: every thread is now pushing all ops, waiting it to complete and then exiting as compared to custom op threads earlier which would just push op to engine and exit. Custom op threads were lightweight earlier and we should keep it that way. |
@anirudh2290 How about leave this PR temporarily and create a new PR to modify the engine code? |
Yes I am fine with that |
close due to issue fixed in #14693 |
Description
This PR fixes Problem 1 in #14522:
It stores exception of custom op and re-throws in sync function.
Checklist
Essentials
Please feel free to remove inapplicable items for your PR.
Changes
Comments